package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.dto.User;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.enums.MediaEnum;
import com.heima.common.minio.service.MinIOService;
import com.heima.common.util.UserThreadLocalUtil;
import com.heima.media.dto.WmMaterialDto;
import com.heima.media.entity.WmMaterial;
import com.heima.media.mapper.WmMaterialMapper;
import com.heima.media.service.IWmMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 自媒体图文素材信息表 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-03-25
 */
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements IWmMaterialService {

    @Autowired
    private MinIOService minIOService;

    /**
     * 将素材上传到MinIO，保存素材记录到素材表中
     *
     * @param file
     * @return
     */
    @Override
    public ResponseResult<WmMaterial> uploadPicture(MultipartFile file) {
        //0.获取登录用户ID
        User user = UserThreadLocalUtil.get();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //1.将素材上传到MinIO
        String uploadFileUrl = minIOService.upload(file);

        //2.保存素材记录到素材表中
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUserId(user.getUserId());
        wmMaterial.setUrl(uploadFileUrl);
        wmMaterial.setIsCollection(false);
        wmMaterial.setType(MediaEnum.MEDIA_MATERIAL_TYPE_IMG.getVal());
        this.save(wmMaterial);
        return ResponseResult.okResult(wmMaterial);
    }

    /**
     * 素材管理需求：单表查询 查询条件：用户id； 收藏状态    分页查询
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult listByCollection(WmMaterialDto dto) {
        User user = UserThreadLocalUtil.get();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //1.构建查询条件
        LambdaQueryWrapper<WmMaterial> queryWrapper = new LambdaQueryWrapper<>();
        //1.1 收藏状态
        if (dto.getIsCollection() != null) {
            queryWrapper.eq(WmMaterial::getIsCollection, dto.getIsCollection());
        }
        //1.2 自媒体用户ID
        queryWrapper.eq(WmMaterial::getUserId, user.getUserId());

        //1.3 排序 上传时间倒序
        queryWrapper.orderByDesc(WmMaterial::getCreatedTime);

        //2.构建分页对象
        IPage<WmMaterial> page = new Page<>(dto.getPage(), dto.getSize());

        //3.执行分页条件查询
        page = this.page(page, queryWrapper);

        //4.封装响应结果
        PageResponseResult<List<WmMaterial>> result = new PageResponseResult<>(dto.getPage(), dto.getSize(), page.getTotal(), page.getRecords());
        return result;
    }
}
